# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html
from sqlalchemy.orm import sessionmaker
from sqlalchemy import create_engine
from .models.models import Job, Company
from .items import JobItem, CompanyItem
from .settings import MYSQL_DBNAME, MYSQL_HOST, MYSQL_PASSWD, MYSQL_PORT, MYSQL_USER


class BosszhipinScrapyPipeline(object):

    def open_spider(self, spider):
        # 初始化数据库连接
        mysql_uri = 'mysql+mysqlconnector://{}:{}@{}:{}/{}'.format(MYSQL_USER, MYSQL_PASSWD, MYSQL_HOST, MYSQL_PORT, MYSQL_DBNAME)
        engine = create_engine(mysql_uri)
        # 创建数据库会话类
        DBSession = sessionmaker(bind=engine)
        self.session = DBSession()
        print('--begin--')

    def process_item(self, item, spider):
        if isinstance(item, JobItem):
            self.process_job_item(item)
        else:
            self.process_company_item(item)
        return item

    def process_job_item(self, item):
        job = Job(
                company=item['company'],
                name=item['name'],
                salary_low=item['salary_low'],
                salary_high=item['salary_high'],
                work_experience=item['work_experience'],
                education=item['education'],
                area=item['area'],
                city=item['city'],
                job_url=item['job_url'],
                tags=item['tags'],
                publish_time=item['publish_time'],
                description=item['description']
            )
        self.session.add(job)
        self.session.commit()

    def process_company_item(self, item):
        company = Company(
            name=item['name'],
            total_name=item['total_name'],
            type=item['type'],
            company_size=item['company_size'],
            company_finance=item['company_finance'],
            description=item['description'],
            environments=item['environments'],
            reg_principal=item['reg_principal'],
            reg_time=item['reg_time'],
            reg_address=item['reg_address'],
            company_url=item['company_url']
        )
        self.session.add(company)
        self.session.commit()


class FormatDataPipeline:

    def process_item(self, item, spider):
        return item.format_data()
